<?php defined('SYSPATH') or die('No direct script access.') ;

class Model_Card_Category extends Model
{

    public function all()
    {
        return DB::select()
            ->from('card_categories')
            ->order_by('sort_order', 'asc')
            ->fetch_all();
    }

    public function keyword($keyword)
    {
        return DB::select()
            ->from('card_categories')
            ->where('keyword', '=', (string) $keyword)
            ->fetch_row();
    }

    public function get($cat_id)
    {
        return DB::select()
            ->from('card_categories')
            ->where('cat_id', '=', (int) $cat_id)
            ->fetch_row();
    }

    public function add(array $data)
    {
        $v = $this->_validate($data);
        $this->validation($v);
        if ( ! $v->check() OR ! empty($this->_errors))
        {
            return FALSE;
        }

        $data = array (
            'cat_name' => $v->cat_name,
            'keyword' => $v->keyword,
            'unit_price' => (float) $v->unit_price,
            'sort_order' => (int) $v->sort_order,
        );

        DB::insert('card_categories', $data)->execute();

        return TRUE;
    }

    public function edit($cat_id, array $data)
    {
        $card = $this->get($cat_id);

        if ( ! $card)
        {
            return $this->error('cat_id', 'not_found');
        }

        $v = $this->_validate($data);
        $this->validation($v);
        if ( ! $v->check() OR ! empty($this->_errors))
        {
            return FALSE;
        }

        $set = array (
            'cat_name' => $v->cat_name,
            'keyword' => $v->keyword,
            'unit_price' => (float) $v->unit_price,
            'sort_order' => (int) $v->sort_order,
        );

        DB::update('card_categories', $set)
            ->where('cat_id', '=', (int) $cat_id)
            ->execute();

        return TRUE;
    }

    public function del($cat_id)
    {
        $count = DB::select()
            ->from('cards')
            ->where('fetch_time', '=', 0)
            ->where('cat_id', '=', (int) $cat_id)
            ->count_all();

        if ($count)
        {
            return $this->error('cat_id', 'not_del');
        }

        DB::delete('card_categories')->where('cat_id', '=', (int) $cat_id)->execute();

        return TRUE;
    }

    protected function _validate(array $data)
    {
        return Validation::factory($data)
            ->rule('cat_name', 'not_empty')
            ->rule('cat_name', 'max_length', array(':value', 50))
            ->rule('keyword', 'not_empty')
            ->rule('keyword', 'regex', array(':value', '#^\w{2,20}$#'))
            ->rule('keyword', 'min_length', array(':value', 2))
            ->rule('keyword', 'max_length', array(':value', 20));
    }

    public function update_stock($cat_id)
    {
        $stocks = DB::select()
            ->from('cards')
            ->where('cat_id', '=', (int) $cat_id)
            ->where('fetch_time', '=', 0)
            ->count_all();

        $sales = DB::select()
            ->from('cards')
            ->where('cat_id', '=', (int) $cat_id)
            ->where('fetch_time', '>', 0)
            ->count_all();

        DB::update('card_categories', array(
                'stocks' => (int) $stocks,
                'sales' => (int) $sales,
            ))
            ->where('cat_id', '=', (int) $cat_id)
            ->execute();
    }

}